<template>
  <a-modal title-align="start" title="设置店长" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="600">
    <a-form auto-label-width ref="formRef" size="medium" :model="form">
      <a-form-item field="shopManager" label="店长" :rules="rules.shopManager">
        <a-select v-model="form.shopManager" placeholder="请选择店长" allow-search allow-clear>
          <a-option :value="item.id" v-for="(item, index) of userList" :key="index">{{ item.userName }}</a-option>
        </a-select>
      </a-form-item>
    </a-form>

    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" :loading="loading" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { defineComponent, ref, computed, onMounted } from 'vue'
  import { getBrandList } from '@/api/product'
  import { Message } from '@arco-design/web-vue'
  import { getOrganizesUser, updateShopBaseBatch } from '@/api/system'
  let props = defineProps(['modelValue', 'type', 'ids'])
  let emits = defineEmits(['save', 'update:modelValue'])
  const formRef = ref()
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })
  let form = ref({
    shopManager: '',
  })
  let userList: any = ref([])
  let loading: any = ref(false)
  let rules = ref({
    shopManager: [{ required: true, message: '请选择店长' }],
  })

  // 获取用户列表
  const getUSerList = async () => {
    let params = {
      depart: false,
      pageNum: 1,
      pageSize: 1000,
    }
    const res: any = await getOrganizesUser(params)
    if (res) {
      userList.value = res.data.records
    }
  }

  const submit = async () => {
    const valid = await formRef.value.validate()
    if (valid) return
    let params = {
      idList: props.ids,
      shopMasterIds: [form.value.shopManager],
    }
    try {
      loading.value = true
      const res: any = await updateShopBaseBatch(params)
      if (res) {
        Message.success('修改成功')
      }
      showvisible.value = false
      emits('save')
    } catch (error) {}
    loading.value = false
  }

  onMounted(() => {
    getUSerList()
  })
</script>

<style lang="less" scoped></style>
